import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class FileDialogDemo(QWidget):
    def __init__(self,parent=None):
        super(FileDialogDemo,self).__init__(parent)
        layout=QVBoxLayout()
        self.btn=QPushButton("加载图片")
        self.btn.clicked.connect(self.getfile)
        layout.addWidget(self.btn)
        self.le=QLabel("")
        layout.addWidget(self.le)
        self.btn1=QPushButton("加载文本文件")
        self.btn1.clicked.connect(self.getfiles)
        layout.addWidget(self.btn1)
        self.contens=QTextEdit()
        layout.addWidget(self.contens)
        self.setLayout(layout)
        self.setWindowTitle("File Dialog 案例")



    def getfile(self):
        fname,_=QFileDialog.getOpenFileName(self,'open file','c:\\',"Image files(*.jpg *.gif)")
        self.le.setPixmap(QPixmap(fname))

    def  getfiles(self):
        dig=QFileDialog()
        dig.setFileMode(QFileDialog.AnyFile)
        dig.setFilter(QDir.Files)
        if dig.exec_():
            filename=dig.selectedFiles()
            f=open(filename[0],'r')
            with f:
                data=f.read()
                self.contens.setText(data)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    demo=FileDialogDemo()
    demo.show()
    sys.exit(app.exec_())